<template>
  <el-dialog :visible="showVisible" center :show-close="false" width="600px">
    <div class="b g3 f16 tl" slot="title">
      <div class="ks-row-between pl15 pr15">
        <span>{{ title }}</span>
        <span
          class="el-icon-close g9 f26 poi"
          @click="showVisible = false"
        ></span>
      </div>
    </div>

    <el-form
      class="pl30 pr30"
      :model="formData"
      ref="form"
      label-width="120px"
      label-position="right"
    >
      <el-form-item
        :label="title"
        prop="ids"
        :rules="[
          { required: true, message: `${$t('sys.please_select')}${title}`, trigger: 'change' }
        ]"
      >
        <tree-select
          width="400"
          filterable
          :value="formData.ids"
          :treeData="tree"
          :multiple="true"
          @change="changes"
          valueString
        />
      </el-form-item>
    </el-form>

    <div slot="footer" class="tr">
      <el-button @click="showVisible = false">{{$t('sys.cancel')}}</el-button>
      <el-button type="primary" :loading="loading" @click="submit">{{$t('sys.confirm')}}</el-button>
    </div>
  </el-dialog>
</template>

<script>
import lodash from 'lodash'

export default {
  props: {
    title: {
      type: String,
      default: ''
    },
    list: {
      type: Array,
      default: () => []
    }
  },
  computed: {
    tree () {
      const { treeData, newIds } = this.disabledTreeNode(
        this.list,
        this.formData.ids,
        this.formData.ids
      )
      return treeData
    }
  },
  data () {
    return {
      loading: false,
      showVisible: false,
      formData: {},
      callback: null,
      disabledIds: []
    }
  },

  methods: {
    open (data = {}, callback = null) {
      this.disabledIds = lodash.cloneDeep(data.ids)
      this.$set(this.formData, 'ids', [])
      this.showVisible = true
      this.callback = callback
      this.$nextTick(() => {
        this.$refs.form.clearValidate()
      })
    },
    async submit () {
      this.loading = true
      try {
        await this.$refs.form.validate()
        const { ids } = this.formData
        this.callback && this.callback(ids)
        this.showVisible = false
      } catch (error) {
        console.log(error)
      } finally {
        this.loading = false
      }
    },

    async changes (v) {
      this.$set(this.formData, 'ids', v || [])
    },
    disabledTreeNode (tree, ids, newIds) {
      const treeData = lodash.cloneDeep(tree).map((node) => {
        node.disabled =
          this.disabledIds.includes(node.id + '') ||
          !!lodash.intersection(
            node._parentIds.map((id) => id + ''),
            this.disabledIds
          ).length ||
          !!lodash.intersection(
            node._parentIds.map((id) => id + ''),
            ids
          ).length ||
          node.accessLevel === 'ReadOnly'
        if (node.disabled && ids.includes(node.id + '')) {
          newIds = newIds.filter((id) => id !== node.id + '')
          this.$set(this.formData, 'ids', newIds || [])
        }
        node.children = this.disabledTreeNode(
          node.children,
          ids,
          newIds
        ).treeData
        return node
      })
      return { treeData, newIds }
    }
  }
}
</script>

<style lang="scss" scoped></style>
